package com.hmkj.core.mapper.account;

import com.hmkj.common.baseMapper.GenericMapper;
import com.hmkj.core.model.simulation.AccountSimulationModel;
import com.hmkj.core.po.account.AccountSimulation;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

/**
 * 模拟账户相关Dao接口<br>
 *
 * @author eproo
 * @date 2018-07-05 09:50:54
 */
public interface AccountSimulationMapper extends GenericMapper<AccountSimulation, Integer> {
    @Select("SELECT t.*,b.user_name AS 'b.userName',b.mobile_phone AS 'b.mobilePhone' FROM biz_account_simulation t LEFT JOIN  biz_user b on t.user_id=b.id WHERE 1=1 ${whereSql} ${sortSql} limit ${nowPage}, ${pageSize} ")
    List<AccountSimulationModel> findListBySqlJoin(Map<String, Object> params);

    @Select("SELECT count(1) FROM biz_account_simulation t LEFT JOIN biz_user b on t.user_id=b.id WHERE 1=1 ${whereSql}")
    long countBySqlJoin(Map<String, Object> params);

    /**
     * 根据用户ID 查询用户账户信息
     *
     * @param userId 用户ID
     * @return
     */
    @Select("SELECT * FROM biz_account WHERE user_id = #{userId} and del_flag = 1")
    AccountSimulation selectByUID(@Param(value = "userId") int userId);

    /**
     * 根据用户ID 更新用户模拟账户资金
     *
     * @param total     总额变化量
     * @param available 可用变化量
     * @param freeze    冻结变化量
     * @param freeze    点买总额变化量
     * @param freeze    费用总额变化量
     * @param freeze    盈利总额变化量
     * @param userId    用户ID
     */
    @Update("UPDATE biz_account_simulation SET " +
            "total = total + (#{total}), " +
            "available = available + (#{available}), " +
            "freeze = freeze + (#{freeze})," +
            "tender_count = tender_count + (#{tenderCount})," +
            "fee_count = fee_count + (#{feeCount})," +
            "profit_count = profit_count + (#{profitCount})," +
            "update_time = now() WHERE  user_id = #{userId} " +
            "AND total + (#{total}) >= 0 " +
            "AND available + (#{available}) >= 0 " +
            "AND freeze + (#{freeze}) >= 0 " +
            "AND tender_count + (#{tenderCount}) >= 0 " +
            "AND fee_count + (#{feeCount}) >= 0 " +
            "AND profit_count + (#{profitCount}) >= 0 ")
    int updateAccountSimulate(@Param(value = "total") BigDecimal total,
                              @Param(value = "available") BigDecimal available,
                              @Param(value = "freeze") BigDecimal freeze,
                              @Param(value = "tenderCount") BigDecimal tenderCount,
                              @Param(value = "feeCount") BigDecimal feeCount,
                              @Param(value = "profitCount") BigDecimal profitCount,
                              @Param(value = "userId") Integer userId);
    /**
     * 根据用户ID 更新平台模拟账户资金（不校验金额正负）
     *
     * @param total     总额变化量
     * @param available 可用变化量
     * @param freeze    冻结变化量
     * @param freeze    点买总额变化量
     * @param freeze    费用总额变化量
     * @param freeze    盈利总额变化量
     * @param userId    用户ID
     */
    @Update("UPDATE biz_account_simulation SET" +
            "total = total + (#{total}), " +
            "available = available + (#{available}), " +
            "freeze = freeze + (#{freeze})," +
            "tender_count = tender_count + (#{tenderCount})," +
            "fee_count = fee_count + (#{feeCount})," +
            "profit_count = profit_count + (#{profitCount})," +
            "update_time = now() WHERE  user_id = #{userId}")
    int updateAccountSimulate4Plat(@Param(value = "total") BigDecimal total,
                                   @Param(value = "available") BigDecimal available,
                                   @Param(value = "freeze") BigDecimal freeze,
                                   @Param(value = "tenderCount") BigDecimal tenderCount,
                                   @Param(value = "feeCount") BigDecimal feeCount,
                                   @Param(value = "profitCount") BigDecimal profitCount,
                                   @Param(value = "userId") Integer userId);

}
